package com.mohe.zhihui.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.zhihui.dao.IDao;
import com.mohe.zhihui.model.Message;
import com.mohe.zhihui.model.Notice;
import com.mohe.zhihui.service.interfaces.IMessage;
import com.mohe.zhihui.utils.Page;

public class MessageImpl implements IMessage {
	
	private IDao<Message> messageDao;

	public IDao<Message> getMessageDao() {
		return messageDao;
	}

	public void setMessageDao(IDao<Message> messageDao) {
		this.messageDao = messageDao;
	}

	@Override
	public List<Message> getMessageList(Message message, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from Message where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StringUtils.isBlank(message.getContent())){
			where.append(" and content like ?");
			params.add("%%"+message.getContent()+"%%");
		}
		where.append(" order by createDate desc");
		List<Message> list = messageDao.query(where.toString(), (page.getCurrPage()-1)*page.getPageSize(), page.getPageSize(), params);
		if(list!=null && list.size()>0){
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(Message message) {
		StringBuffer where = new StringBuffer();
		where.append("from Message where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StringUtils.isBlank(message.getContent())){
			where.append(" and content like ?");
			params.add("%%" + message.getContent() + "%%");
		}
		return messageDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveMessage(Message message) {
		return messageDao.insert(message);
	}

	@Override
	public Message getMessageById(Long timeKey) {
		Message message = messageDao.getByKey(timeKey);
		if(message != null){
			return message;
		}
		return null;
	}

	@Override
	public boolean updateMessage(Message message) {
		return messageDao.update(message);
	}

	@Override
	public boolean delMessage(Message message) {
		return messageDao.delete(message.getTimeKey());
	}

	@Override
	public List<Message> getAllMessage() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return messageDao.query(where.toString(), null);
	}

}
